import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.TooManyListenersException;

import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;

public
class Serial2 implements SerialPortEventListener {
    // 串口
  private
    static SerialPort serialPort = null;
  private
    static CommPortIdentifier comm = null;
  private
    static BufferedInputStream bufferInput;
  public
    Serial2() {
        try {
            // 获取通信端口标识符
            comm = Serial.getCommPortIdentifierByName("COM3");
            // 打开串口
            serialPort = (SerialPort)comm.open("Read", 2000);
            // 设置串口的参数 波特率 数据位 停止位 奇偶效验
            serialPort.setSerialPortParams(
                Serial.getBaudRate(), Serial.getDataBits(),
                Serial.getStopBits(), Serial.getParity());
            // 如果串口不为空的话 则监听获取信息 否则报错
            OutputStream output = null;
            if (serialPort != null) {
                // 获取输入输出流
                bufferInput =
                    new BufferedInputStream(serialPort.getInputStream());
                output = serialPort.getOutputStream();
                // 添加监听
                serialPort.addEventListener(this);
                serialPort.notifyOnDataAvailable(true);
                // 发送数据
                char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7',
                              '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
                Serial.sendData(output, Protocal.getDataHead(), "52 52 52 01",
                                Protocal.getCRC(), Protocal.getDataTail());
                System.out.println(Protocal.getCRC());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

  public
    static void main(String[] args) { new Serial2(); }

    /**
     * -串口的监听
     */
    @Override public void serialEvent(SerialPortEvent ev) {
        // 判断数据类型
        switch (ev.getEventType()) {
        // 通讯中断 Break Interrupt
        case SerialPortEvent.BI:
            System.out.println("BI");
            break;
        // 溢位错误 Overrun Error
        case SerialPortEvent.OE:
            System.out.println("OE");
            break;
        // 传帧错误 Framing Error
        case SerialPortEvent.FE:
            System.out.println("FE");
            break;
        // 效验错误 Parity Error
        case SerialPortEvent.PE:
            System.out.println("PE");
            break;
        // 载波检测 Carrier Detect
        case SerialPortEvent.CD:
            System.out.println("CD");
            break;
        // 清除发送 Clear To Send
        case SerialPortEvent.CTS:
            System.out.println("CTS");
            break;
        // 数据设备就绪 Data Set Ready
        case SerialPortEvent.DSR:
            System.out.println("DSR");
            break;
        // 响铃指示 Ring Indicator
        case SerialPortEvent.RI:
            System.out.println("RI");
            break;
        // 输出缓冲区清空 Output Buffer Empty
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            System.out.println("output");
            break;
        // 端口有可用数据 Data Available
        case SerialPortEvent.DATA_AVAILABLE:
            // 读取数据
            try {
                byte[] bytes = null;
                byte[] tempBytes = new byte[1024];
                int len = -1;
                while ((len = bufferInput.read(tempBytes)) != -1) {
                    // 将数据写入bytes中 并更改bytes的长度
                    if (bytes != null) {
                        byte[] b = bytes;
                        bytes = new byte[bytes.length + len];
                        for (int i = 0; i < bytes.length; i++) {
                            if (i < len) {
                                bytes[i] = b[i];
                            } else {
                                bytes[i] = tempBytes[i];
                            }
                        }
                    } else {
                        bytes = new byte[len];
                        for (int i = 0; i < bytes.length; i++) {
                            bytes[i] = tempBytes[i];
                        }
                    }
                }
                // 输出数据
                System.out.println("字符串形式:" + new String(bytes));
                System.out.println("16进制形式:" +
                                   Protocal.switchByteToHexadecimal(bytes));
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        }
    }
}